#include "widget.h"
#include "ui_widget.h"

#include <QLabel>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QLabel* lb1 = new QLabel(ui->tab);
    lb1->setText("标签页1");
    lb1->resize(100, 50);

    QLabel* lb2 = new QLabel(ui->tab_2);
    lb2->setText("标签页2");
    lb2->resize(100, 50);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    // 创建标签页，参数1widget，参数2标题
    QWidget* w = new QWidget();
    // 获取当前第几个标签页
    int count = ui->tabWidget->count();
    ui->tabWidget->addTab(w, "Tab " + QString::number(count + 1));
    // 添加label
    QLabel* lb = new QLabel(w);
    lb->setText("标签 " + QString::number(count + 1));
    lb->resize(100, 50);
    // 选中新建的标签页
    ui->tabWidget->setCurrentIndex(count);
}

void Widget::on_pushButton_2_clicked()
{
    int index = ui->tabWidget->currentIndex();
    ui->tabWidget->removeTab(index);
}

void Widget::on_tabWidget_currentChanged(int index)
{
    qDebug() << index;
}
